package com.zh.api.controller.servicer;


import com.zh.common.utils.*;
import com.zh.servicer.domain.BasicCompanyDO;
import com.zh.servicer.domain.OcServicerTypeDO;
import com.zh.servicer.domain.ServiceDO;
import com.zh.servicer.service.BasicCompanyService;
import com.zh.servicer.service.OcServicerTypeService;
import com.zh.servicer.service.ServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by caogang on 2019/5/28.
 */


@Controller
@RequestMapping("/servicer")
@Api("服务商管理")
public class ServicerController {


    @Autowired
    private BasicCompanyService basicCompanyServcice;
    @Autowired
    private OcServicerTypeService ocServicerTypeService;
    @Autowired
    private ServiceService serviceService;


    @ResponseBody
    @GetMapping("/list")
    @ApiOperation("获取服务商列表")
    public PageUtils list(@RequestParam Map<String, Object> params){

        //查询列表数据
        Query query = new Query(params);
        List<BasicCompanyDO> basicCompanyList = basicCompanyServcice.list(query);
        int total = basicCompanyServcice.count(query);
        PageUtils pageUtils = new PageUtils(basicCompanyList, total);
        return pageUtils;

    }



    @ResponseBody
    @GetMapping( "/getServicerType")
    @ApiOperation("获取服务商类别名称")
    ResultVO getServicerType(){
        Map map =new HashMap<>();
        List<OcServicerTypeDO>  typeList=ocServicerTypeService.list(map);
        return ResultVO.success(typeList);
    }



    /**
     * 保存服务商
     * @param basicCompany
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    @ApiOperation("保存服务商")
    public ResultVO save(BasicCompanyDO basicCompany) {
        Map<String, Object> map = new HashMap<>();
        basicCompany.setId(StringUtil.generalKeyword());
       if(basicCompanyServcice.save(basicCompany)>0){
           map.put("companyId",basicCompany.getId());
           return ResultVO.success(map);
       }
        return ResultVO.error();

    }



    /**
     * 根据id获取服务商信息
     * @param id
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("/getServicerInfo")
    @ApiOperation("根据id获取服务商信息")
    ResultVO getServicerInfo(String id){
        Map map=new HashMap();
        BasicCompanyDO basicCompany = basicCompanyServcice.get(id);
        map.put("data",basicCompany);
        return  ResultVO.success(map);
    }


    /**
     * 修改服务商
     */
    @ResponseBody
    @RequestMapping("/update")
    @ApiOperation("修改服务商")
    public ResultVO update(BasicCompanyDO basicCompany){
        if(basicCompanyServcice.update(basicCompany)>0) {
            return  ResultVO.success();
        }
        return   ResultVO.error();
    }


    /**
     * 删除服务商
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping( "/remove")
    @ApiOperation("删除服务商")
    public ResultVO remove(String id){
        if(basicCompanyServcice.remove(id)>0){
            return ResultVO.success();
        }
        return ResultVO.error();
    }

    /**
     * 批量删除服务商
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping( "/batchRemove")
    @ApiOperation("批量删除服务商")
    public ResultVO remove(@RequestParam("ids[]") String[] ids) {
        basicCompanyServcice.batchRemove(ids);
        return ResultVO.success();
    }

    /**
     * 获取服务商名称
     * @return
     */
    @ResponseBody
    @GetMapping( "/getAllServicerName")
    @ApiOperation("获取服务商名称")
    public ResultVO getAllServicerName(){
        Map map =new HashMap();
        List<BasicCompanyDO>  allServicerName=basicCompanyServcice.list(map);
        return ResultVO.success(allServicerName);
    }


    /**
     * 保存服务商人员
     * @param staff
     * @return
     */
    @ResponseBody
    @PostMapping("/saveServicerStaff")
    @ApiOperation("保存服务商人员")
    ResultVO  saveServicerStaff(ServiceDO serviceDO) {

        serviceService.saveServicerStaff(serviceDO);
        return ResultVO.success();
    }


}
